package com.android.monee.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import com.android.monee.data.access.DaoFactory;
import com.android.monee.data.access.DaoListener;
import com.android.monee.data.access.MensualityDao;
import com.android.monee.data.access.inMemory.ObservableDao;
import com.android.monee.data.model.Budget;
import com.android.monee.data.model.Mensuality;

public class MensualityService extends Mensuality {
	
	public MensualityService(Mensuality mensuality) {
		super(mensuality); 
	}

//	public static List<Mensuality> calcNextMensualities(int budgetId){
//		List<Mensuality> nextMensualities = new ArrayList<Mensuality>();
//		
//		BudgetService budget = BudgetService.findById(budgetId);
//					
//		float amountToDistribute = budget.getRemainingAmount();
//		int nbMensToDistribute = budget.getRemainingNbMens();
//		int ceiledMensAmount = (int) Math.ceil(amountToDistribute/nbMensToDistribute);
//		
//		Set<Integer> nextMensNumbers = getNextMensNumbers(budget);
//		
//		for(Integer i : nextMensNumbers){
//			
//			float mensAmount = 0;			
//			if(i == budget.getTotalNbMens())
//				mensAmount = amountToDistribute;
//			else
//				mensAmount = ceiledMensAmount;
//
//			amountToDistribute -= mensAmount;
//			
//			Mensuality mens = new Mensuality();
//			mens.setBudgetId(budgetId);
//			mens.setNumber(i);
//			mens.setAmount(mensAmount);
//			mens.setDate(budget.getDateForMensuality(mens.getNumber()));
//			
//			nextMensualities.add(mens);
//		}
//		
//		return nextMensualities;
//	}
	
//	private static Set<Integer> getNextMensNumbers(BudgetService budget){
//		SortedSet<Integer> allMensNumbers = new TreeSet<Integer>();
//		for(int i = 1 ; i <= budget.getTotalNbMens() ; i++){
//			allMensNumbers.add(i);
//		}
//		
//		List<MensualityService> doneMensualities = budget.getDoneMensualities();
//		for(MensualityService mens : doneMensualities){
//			allMensNumbers.remove(mens.getNumber());
//		}
//		
//		return allMensNumbers;
//	}
	
	//Question : quel est le mieux ? cette version ou celle dans MensualityCalculator ?
	public static List<Mensuality> calcNextMensualities(){
		List<BudgetService> budgets = BudgetService.findAll();
		List<Mensuality> nextMensualities = new ArrayList<Mensuality>();
		
		for(BudgetService budget : budgets){
			nextMensualities.addAll(budget.calcNextMensualities());
		}
		return nextMensualities;
	}
	
	public static List<Mensuality> getDoneMensualities(){
		List<BudgetService> budgets = BudgetService.findAll();
		List<Mensuality> doneMensualities = new ArrayList<Mensuality>();
		
		for(BudgetService budget : budgets){
			doneMensualities.addAll(budget.getDoneMensualities());
		}
		return doneMensualities;
	}
	
	public static List<MensualityService> findByBudget(Budget budget){
		MensualityDao dao = DaoFactory.getMensualityDao();
		return newCollection(dao.findByBudget(budget));
	}
	
	public static List<MensualityService> findByBudgetAndState(Budget budget, MensualityState state){
		MensualityDao dao = DaoFactory.getMensualityDao();
		return newCollection(dao.findByBudgetAndState(budget, state));
	}
	
	public static void insert(Mensuality mens){
		MensualityDao dao = DaoFactory.getMensualityDao();
		dao.insert(mens);
	}

	public static void update(Mensuality mens){
		MensualityDao dao = DaoFactory.getMensualityDao();
		dao.update(mens);
	}
	
	public static void delete(Mensuality mens){
		MensualityDao dao = DaoFactory.getMensualityDao();
		dao.delete(mens.getId());
	}
	
	public static boolean addBudgetDaoListener (DaoListener<Mensuality, Integer> listener){
		MensualityDao dao = DaoFactory.getMensualityDao();
		if(dao instanceof ObservableDao){
			ObservableDao<Mensuality, Integer> observableDao = (ObservableDao<Mensuality, Integer>) dao;
			observableDao.addDaoListener(listener);
			return true;
		}
		else 
			return  false;
	}
	
	public static boolean deleteBudgetDaoListener (DaoListener<Mensuality, Integer> listener){
		MensualityDao dao = DaoFactory.getMensualityDao();
		if(dao instanceof ObservableDao){
			ObservableDao<Mensuality, Integer> observableDao = (ObservableDao<Mensuality, Integer>) dao;
			observableDao.deleteDaoListener(listener);
			return true;
		}
		else 
			return false;
	}
	
	public static List<MensualityService> newCollection(Collection<? extends Mensuality> mensualities){
		List<MensualityService> newCol = new ArrayList<MensualityService>();
		for(Mensuality mensuality : mensualities){
			newCol.add(new MensualityService(mensuality));
		}
		return newCol;
	}
}
